package net.wanho.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Author：汤小洋
 * Date：2025-09-02 16:45
 * Description：<描述>
 */
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("接收到用户的请求啦。。。。。。。。。。。。");

        req.setCharacterEncoding("utf-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        int age = Integer.parseInt(req.getParameter("age"));
        String name = req.getParameter("name");
        System.out.println("用户名：" + username);
        System.out.println("密码：" + password);
        System.out.println("年龄：" + age);
        System.out.println("姓名：" + name);
        System.out.println("请求方式：" + req.getMethod());

        // 指定响应内容类型和编码
        // resp.setContentType("text/html; charset=utf-8");
        // PrintWriter out = resp.getWriter();
        // out.print("<html>");
        // out.print("<head><title>注册响应</title></head>");
        // out.print("<body>");
        // out.print("<h2 style='color:red;'>注册成功</h2>");
        // out.print("<h2 style='color:red;'>用户名：" + username + "</h2>");
        // out.print("</body>");
        // out.print("</html>");
        // out.close();

        resp.setContentType("text/plain; charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print("成功！");
        out.close();

        // resp.sendRedirect("index.html");
    }
}
